<?php

	//this file provides methods for getting stuff out of urls and what not..

	
	function extractValueWithKeyFromArray($key, &$array, $default = null){
		if (is_array($array) && isset($array[$key]) && !is_null($array[$key])){
			return $array[$key];
		}
		return $default;
	}
	
	function extractNumberWithKeyFromArray( $key, &$array, $default = null ) {
		$value = extractValueWithKeyFromArray($key, $array, $default);
		if ($value === $default) {
			return $default;
		} else if (is_numeric($value))
			return $value * 1;//we do this to ensure we get a fucking number back and not a string.. fucking floppy typecasting shitness
				
		return $default;
	}
	
	function extractBooleanWithKeyFromArray( $key, $array, $default = false ) {
		$value = magicClean( extractValueWithKeyFromArray($key, $array, $default), false);
		if (is_bool($value) && $value === $default) {
			return $default;
		} else if (is_bool($value)) {
			return $value;
		} else if (is_null($value)) {
			return $default;
		} else if (is_numeric($value))
			return $value > 0;
		else if( is_string($value)) {
			$value = strtoupper($value);
			return ( $value == "1" || $value == "YES" || $value == "TRUE");
		}
				
		return $default;
	}
	
	
	
	function extractStringWithKeyFromArray( $key, $array, $default = null ) {
		$value = extractValueWithKeyFromArray($key, $array, $default);
		if ($value === $default) {
			return $default;
		} else if (is_string($value) && strlen($value) > 0 ) 
			return $value;
		else if (is_numeric($value)){
			$value = $value . "";
			if (is_string($value) && strlen($value) > 0 ) 
				return $value;
		}
		return $default;
	}
	
	function extractDateWithKeyFromArray($key, $array, $default = null) {
		$val = extractStringWithKeyFromArray($key, $array, $default);
		if (is_date($val)) return $value;
		return $default;
	}
	
	function extractObjectWithKeyOfClassFromArray($key, $class, $array, $default = null ){
		$o = extractObjectWithKeyFromArray($key, $array, $default);
		//error_log(get_class($o));
		if (!is_null($o) && (is_a($o,$class)|| is_subclass_of($o,$class)  )) {
				return $o;
		}
		return $default;
	}
	
	function extractObjectWithKeyFromArray( $key, $array, $default = null ) {
		$value = extractValueWithKeyFromArray($key, $array, $default);
		if ($value === $default) {
			return $default;
		} else if (is_object($value))
			return $value;
			
		return $default;
	}
	
	function extractArrayWithKeyFromArray( $key, $array, $default = null ) {
		//if ( is_array($array) && isset($array[$key]) && is_array( $array[$key] )) {
		//	return $array[$key];
		//}
		$value = extractValueWithKeyFromArray($key, $array, $default);
		if ($value === $default) {
			return $default;
		} else if (is_array($value))
			return $value;
			
		return $default;
	}
	
	function instanceOf($object, $class) {
		//$oClass = get_class( $object );
		//return $class == $oClass;
		return is_a($object, $class	);
	}
	

	
	function registerSessionPseudoSuperGlobal($sessionPseudoSuperGlobalName, $name, &$value){
			if (!isset($_SESSION))
				return $value;
				
			$a = extractArrayWithKeyFromArray($sessionPseudoSuperGlobalName,$_SESSION,null);
			if (is_null($a)){
				$a = array();
			}
			$a[$name] = $value;
			$_SESSION[$sessionPseudoSuperGlobalName] = $a;
			
//			error_log("registerSessionPseudoSuperGlobal:   " . $sessionPseudoSuperGlobalName . "=>" .$name . "=>". $value);
			
	}
	
	function clearSessionPseudoSuperGlobal($sessionPseudoSuperGlobalName, $name){
		if (!isset($_SESSION))
			return;
					
		$a = extractArrayWithKeyFromArray($sessionPseudoSuperGlobalName,$_SESSION,array());
		unset( $a[$name] ) ;
//		error_log("clearSessionPseudoSuperGlobal:   " . $sessionPseudoSuperGlobalName . "=>" . $name);
	}
	
	
	//function ()..
	
	define ("EXTRACT_STRING","string");
	define ("EXTRACT_STRING_FUNCTION","extractStringWithKeyFromArray");
	
	define ("EXTRACT_NUMBER","number");
	define ("EXTRACT_NUMBER_FUNCTION","extractNumberWithKeyFromArray");
	
	define ("EXTRACT_ARRAY","array");
	define ("EXTRACT_ARRAY_FUNCTION", "extractArrayWithKeyFromArray");
	
	define ("EXTRACT_DATE","date");
	define ("EXTRACT_DATE_FUNCTION", "extractStringWithKeyFromArray");//dates are extracted as is..but only returned if they are a valid date
	
	define ("EXTRACT_BOOLEAN", "bool");
	define ("EXTRACT_BOOLEAN_FUNCTION", "extractBooleanWithKeyFromArray");
	
	define ("EXTRACT_OBJECT", "object");
	define ("EXTRACT_OBJECT_FUNCTION", "extractObjectWithKeyFromArray");
	
	define ("EXTRACT_FILE_UPLOAD","file");
	
	define ("EXTRACT_VALUE","value");
	define ("EXTRACT_VALUE_FUNCTION", "extractValueWithKeyFromArray");
	
	
	// now we have a predicament.. where we need to handle certain keys smartly
	// for joins / many to many
	// these are special defines for many to many and one to many extraction...
	// for when loading etc..
	define("EXTRACT_TO_MANY_OBJECTS","obejctToMany");
	
	define("EXTRACT_TO_ONE_OBJECT","obejctToOne");

?>